var url = require("url");

module.exports.getFullUrl = function(cUrl, rUrl) {
	if (rUrl.indexOf('http') == 0) return rUrl;

	var ctx = url.parse(cUrl);
	var cPath = ctx.pathname;

	if (rUrl.indexOf('/') == 0) {
		cPath = rUrl;
	} else {
		// a/b/c.html --- x.html
		if (cPath.length > 1 && /\/$/.test(cPath)) {
			cPath = cPath.substring(0, cPath.length - 2);
		}
		// console.log(ctx.pathname);
		var lastSlash = cPath.lastIndexOf("/");
		cPath = cPath.substring(0, lastSlash + 1) + rUrl;
	}
	ctx.pathname = cPath;
	return url.format(ctx);
}

var re = /href\s*?=\s*?"([^"]+?)"/g;
module.exports.getHrefInHtml = function(html) {
	var list = html.match(re);
	return list;
}

// 
var re2 = /href\s*?=\s*?"([^"]+?)"/;
module.exports.urlFilter = function(list, blacklist) {
	// console.log('urlFilter', blacklist);
	if(!list){
		return null;
	}
	
	var nList = list.map(function(item) {
		return item.match(re2)[1]; // href='xxx' => xxx
	});

	nList = nList.filter(function(item) {
		var valid = true;
		var index = 0;
		while (valid && index < blacklist.length) {
			var regex = new RegExp(blacklist[index]);
			if (regex.test(item)) {
				valid = false;
			}
			index++;
		}
		if (valid) {
			// console.log('valid', item);
		} else {
			// console.log('not valid', item);
		}
		return valid;
	});
	return nList;
}